package com.example.yilu1;

import android.content.Context;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gc.materialdesign.views.CheckBox;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.List;

public class GoodAdapter extends ArrayAdapter<Good> {
    private int resourceId;
    private Good good;
    private android.widget.CheckBox checkBox;

    public GoodAdapter(Context context, int textViewResourceId,List<Good> objects){
        //重写父类的构造函数，将上下文、ListView子项布局的id和数据都传递进来
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        //重写getView方法，这个方法在每个子项被滚动到屏幕内是会被调用
        good=getItem(position);//得到当前项的Good实例
        View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        //用LayoutInflater的inflate方法为这个子项加载布局，false表示不会为这个View添加父布局
        checkBox= (android.widget.CheckBox) view.findViewById(R.id.good_chechbox);
        ImageView goodImage=(ImageView)view.findViewById(R.id.good_image);
        TextView goodName=(TextView)view.findViewById(R.id.good_name);
        ImageView platformImage=(ImageView)view.findViewById(R.id.platform_image);
        TextView shopName=(TextView)view.findViewById(R.id.shop_name);
//        checkBox.setOncheckListener(new CheckBox.OnCheckListener() {
//            @Override
//            public void onCheck(CheckBox view, boolean check) {
//                assert good != null;
//                if(good.getIsSelected()) {
//                //if(!checkBox.isCheck()){
//                    checkBox.setChecked(true);
//                } else checkBox.setChecked(false);
//            }
//        });
        assert good != null;
        //notifyDataSetChanged();
        if(good.getIsSelected())checkBox.setChecked(true);
        else checkBox.setChecked(false);
        goodImage.setImageResource(good.getImageId());
        goodName.setText(good.getName());
        platformImage.setImageResource(good.getPlatformImageId());
        shopName.setText(good.getShopName());
        return view;    //最后将布局返回
    }
}